package juc.blockqueue.aynchronousqueue;

/**
 * @ClassName OrderPaymentMatcher
 * @PackageName juc.blockqueue.aynchronousqueue
 * @projectName myThread
 * @Description 3. 生产者-消费者实时匹配
 * @Author games
 * @Date 2025/9/4 下午1:59
 * @Version 1.0
 */
import java.util.concurrent.SynchronousQueue;
import java.util.Random;

class Order {
    private final String orderId;
    private final double amount;

    public Order(String orderId, double amount) {
        this.orderId = orderId;
        this.amount = amount;
    }

    public String getOrderId() { return orderId; }
    public double getAmount() { return amount; }

    @Override
    public String toString() {
        return "Order{orderId='" + orderId + "', amount=" + amount + "}";
    }
}

class Payment {
    private final String paymentId;
    private final double amount;

    public Payment(String paymentId, double amount) {
        this.paymentId = paymentId;
        this.amount = amount;
    }

    public String getPaymentId() { return paymentId; }
    public double getAmount() { return amount; }

    @Override
    public String toString() {
        return "Payment{paymentId='" + paymentId + "', amount=" + amount + "}";
    }
}

public class OrderPaymentMatcher {
    public static void main(String[] args) {
        SynchronousQueue<Order> orderQueue = new SynchronousQueue<>();
        SynchronousQueue<Payment> paymentQueue = new SynchronousQueue<>();
        Random random = new Random();

        // 订单生成器
        new Thread(() -> {
            try {
                for (int i = 1; i <= 5; i++) {
                    Order order = new Order("ORDER-" + i, 100 + random.nextInt(900));
                    System.out.println("生成订单: " + order);
                    orderQueue.put(order);
                    Thread.sleep(1000 + random.nextInt(2000));
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();

        // 支付生成器
        new Thread(() -> {
            try {
                for (int i = 1; i <= 5; i++) {
                    Payment payment = new Payment("PAY-" + i, 100 + random.nextInt(900));
                    System.out.println("生成支付: " + payment);
                    paymentQueue.put(payment);
                    Thread.sleep(500 + random.nextInt(1500));
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();

        // 匹配器
        new Thread(() -> {
            try {
                while (true) {
                    // 同时等待订单和支付
                    Order order = orderQueue.take();
                    Payment payment = paymentQueue.take();

                    System.out.println("匹配成功: " + order.getOrderId() +
                            " 与 " + payment.getPaymentId() +
                            " (金额: " + order.getAmount() + ")");
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
}

